/**
 * This activity represents file-opening dialog
 */
package org.amse.ys.sndeditor.ui;

import java.io.File;
import java.util.ArrayList;

import org.amse.ys.sndeditor.model.Localiser;
import org.amse.ys.sndeditor.ui.R.string;

import android.app.*;
import android.view.*;
import android.view.View.OnKeyListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FileOpener extends Activity {
	public static String FILE_NAME = "nameOfFile";
	private final ArrayList<String> myFilesList = new ArrayList<String>();
	private ArrayAdapter<String> myAa;
	private EditText myEditText;
	private File myCurrentFolder;

	public void fileReady(String name) {
		Intent intent = new Intent();
		intent.setClass(this, Soundeditor.class);
		intent.putExtra(FileOpener.FILE_NAME, name);
		startActivity(intent);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0,
				Localiser.getString(string.switch_to_media_browser)).setIcon(
				org.amse.ys.sndeditor.ui.R.drawable.media_browser);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == Menu.FIRST) {
			Intent intent = new Intent();
			intent.setClass(this, SoundOpenActivity.class);
			startActivity(intent);
			finish();
			return true;
		}
		return false;
	}

	// this method should get absolute path
	private void fillWithFiles(String fileName) {
		myFilesList.clear();

		File file = new File(fileName);
		myCurrentFolder = file;
		myEditText.setText(fileName);

		File[] fileList = file.listFiles();
		if (!"/".equals(fileName)) {
			myFilesList.add("..");
		}

		for (File tmpFile : fileList) {
			if (tmpFile.isDirectory()) {
				myFilesList.add(tmpFile.getName() + "/");
			}
		}
		for (File tmpFile : fileList) {
			if (!tmpFile.isDirectory()) {
				myFilesList.add(tmpFile.getName());
			}
		}

		myAa.notifyDataSetChanged();

	}

	private void tryToOpen(boolean isAbsolute, String fileName) {
		// convert fileName
		if (!isAbsolute) {
			if (fileName == null) {
				fileName = myCurrentFolder.getPath();
			} else if ("..".equals(fileName)) {
				fileName = myCurrentFolder.getParent();
				if (fileName == null) {
					fileName = "/";
				}
			} else {
				fileName = myCurrentFolder.getAbsolutePath() + "/" + fileName;
			}
		}
		File file = new File(fileName);
		if (!file.exists()) {
			return;
		}
		if (file.isDirectory()) {
			fillWithFiles(file.getAbsolutePath());
		} else {
			fileReady(fileName);
		}

	}

	@Override
	public void onCreate(Bundle icicle) {
		// init activity, set listeners
		super.onCreate(icicle);
		setContentView(R.layout.file_opener);
		ListView myListView = (ListView) findViewById(R.id.myListView);
		myEditText = (EditText) findViewById(R.id.myEditText);

		myAa = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, myFilesList);

		myListView.setAdapter(myAa);

		myEditText.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {

					if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
							|| (keyCode == KeyEvent.KEYCODE_ENTER)) {
						tryToOpen(true, (myEditText.getText().toString()));
						return true;
					}
				}
				return false;
			}
		});

		myListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				tryToOpen(false, myFilesList.get(arg2));
			}
		});

		// default folder
		myCurrentFolder = new File("/");
		tryToOpen(false, null);

	}

}
